package src.main.java.com.pms.gui.dialog;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSet;
import src.main.java.com.pms.service.PetBasicService;

public class PetListDialog extends JDialog {
    private JTable petTable;
    private DefaultTableModel tableModel;
    private PetBasicService petService;

    public PetListDialog(JFrame parent) {
        super(parent, "宠物列表", true);
        petService = new PetBasicService();
        initComponents();
        loadPetData();
    }

    private void initComponents() {
        setSize(600, 400);
        setLocationRelativeTo(getParent());

        // 创建表格模型
        String[] columnNames = {"ID", "名称", "类型", "年龄", "状态"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        petTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(petTable);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton refreshButton = new JButton("刷新");
        JButton deleteButton = new JButton("删除");
        JButton editButton = new JButton("编辑");

        refreshButton.addActionListener(e -> loadPetData());
        deleteButton.addActionListener(e -> deletePet());
        editButton.addActionListener(e -> editPet());

        buttonPanel.add(refreshButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(editButton);

        // 设置布局
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void loadPetData() {
        try {
            // 清空表格
            tableModel.setRowCount(0);
            
            // 获取宠物数据
            ResultSet rs = petService.getAllPets();
            while (rs.next()) {
                Object[] row = {
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getString("type"),
                    rs.getInt("age"),
                    getStatusString(rs)
                };
                tableModel.addRow(row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载数据失败：" + e.getMessage(), 
                "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private String getStatusString(ResultSet rs) throws Exception {
        StringBuilder status = new StringBuilder();
        if (rs.getInt("has_bath_appointment") == 1) status.append("洗澡预约中 ");
        if (rs.getInt("has_vaccine_appointment") == 1) status.append("疫苗预约中 ");
        if (rs.getInt("is_for_sale") == 1) status.append("寄卖中 ");
        if (rs.getInt("has_cremation_appointment") == 1) status.append("火化预约中");
        return status.toString().trim();
    }

    private void deletePet() {
        int selectedRow = petTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要删除的宠物！", 
                "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(this, 
            "确定要删除这只宠物吗？", "确认删除", 
            JOptionPane.YES_NO_OPTION);

        if (confirm == JOptionPane.YES_OPTION) {
            try {
                if (petService.deletePet(id)) {
                    loadPetData();
                    JOptionPane.showMessageDialog(this, "删除成功！");
                } else {
                    JOptionPane.showMessageDialog(this, "删除失败！", 
                        "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "删除失败：" + e.getMessage(), 
                    "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void editPet() {
        int selectedRow = petTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要编辑的宠物！", 
                "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        new EditPetDialog(this, id).setVisible(true);
        loadPetData(); // 刷新数据
    }
} 